﻿class Line extends MovieClip{
	public var n1:MovieClip;
	public var n2:MovieClip;
	
	public var v:Vector;
	
	public var norm:MovieClip;
	public var proj:MovieClip;
	public var projXY:MovieClip;
	
	public function init(p1:Point, p2:Point) {
		//MCs n1 and n2 are the start and end points of the Line
		
		n1 = _root.attachMovie("Node", "Node"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
		n1.init(p1.x, p1.y);
		n1.addLine(this);
		
		n2 = _root.attachMovie("Node", "Node"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
		n2.init(p2.x, p2.y);
		n2.addLine(this);
		
		v = new Vector( p1, (p2.x - p1.x), (p2.y - p1.y) );
		
		norm = _root.createEmptyMovieClip("norm" ,_root.getNextHighestDepth());
		proj = _root.createEmptyMovieClip("proj" ,_root.getNextHighestDepth());
		projXY = _root.createEmptyMovieClip("projXY" ,_root.getNextHighestDepth());
		
		update();
	}
	
	public function update() {
		this.clear();
		this.lineStyle(2,0x0000FF,100);
		this.moveTo(n1._x, n1._y);
		this.lineTo(n2._x, n2._y);
		
		v.setPoint( new Point(n1._x, n1._y) );
		v.setXY( (n2._x - n1._x), (n2._y - n1._y) );
		
		dispNormal();
		dispXY();
	}
	
	/*
	* Extra Display Functions
	*/
	
	//displays the projection of a vector onto this line segment
	public function dispProject(v2:Vector):Void {
		var p:Vector = _root.project(v, v2);
		
		proj.clear();
		proj.lineStyle(5,0xFF00FF,100);		
		proj.moveTo(p.p.x, p.p.y);
		proj.lineTo(p.p.x + p.dx, p.p.y + p.dy);
	}
	
	//displays the X- and Y-components of the line segments
	public function dispXY():Void {
		projXY.clear();
		projXY.lineStyle(1,0x00FF00,100);		
		projXY.moveTo(n1._x, n1._y);
		projXY.lineTo(n1._x + v.dx, n1._y);
		projXY.lineTo(n2._x, n2._y);
	}
	
	//displays this line segment's normal
	public function dispNormal():Void {
		var n:Vector = getNormal();
		
		n.setLength(50);
		
		norm.clear();
		norm.lineStyle(1,0xFF0000,100);		
		norm.moveTo(n.p.x, n.p.y);
		norm.lineTo(n.p.x + n.dx, n.p.y + n.dy);
	}
	
	public function getNormal():Vector {
		var n:Vector = v.getNormal();
		
		return n;
	}
}